﻿using System;
using System.Collections.Generic;
using EXT;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using DevExpress.Utils;

namespace BanHangCshap.DanhMuc.gkhtdl63.Banhang
{
    public partial class frmTraNCC : Form
    {
        public frmTraNCC()
        {
            InitializeComponent();
        }

        Class.gkhtdl63.BLL.Phieuxuat2BLL xgPhieuxuat2BLL = new BanHangCshap.Class.gkhtdl63.BLL.Phieuxuat2BLL();
        Class.gkhtdl63.BLL.Phieuxuat2ChitietBLL xgPhieuxuat2ChitietBLL = new BanHangCshap.Class.gkhtdl63.BLL.Phieuxuat2ChitietBLL();
        private DataTable xgDTQCBoSanpham = new DataTable();
        private long xgId_Phieuxuat2 = -1;
        private string xgMa_NhanvienLap = string.Empty;

        /// <summary>
        /// Biến kiểm tra xem là đang thêm hay sữa
        /// </summary>
        private bool xgIsAOrE = true;
        DateTime xgNgay;


        private void Hienthi()
        {
            DataTable xlDT = xgPhieuxuat2BLL.Phieuxuat2_LayTheoId(xgId_Phieuxuat2);
            if (xlDT != null)
            {
                if (xlDT.Rows.Count > 0)
                {
                    dtpNgay.EditValue = xlDT.Rows[0]["Ngay"].ToDateTime();
                    xgNgay = dtpNgay.EditValue.ToDateTime();
                    txtMa.EditValue = xlDT.Rows[0]["Ma"].ToString();
                    gleKhachhang2.EditValue = null;
                    gleKhachhang2.EditValue = (long)xlDT.Rows[0]["Id_Khachhang2"].ToFloat();
                    gleMa_NhanvienLap.EditValue = xlDT.Rows[0]["Ma_NhanvienLap"].ToString();
                    gleMa_NhanvienGiao.EditValue = xlDT.Rows[0]["Ma_NhanvienGiao"].ToString();
                    txtGhichu.EditValue = xlDT.Rows[0]["Ghichu"].ToString();
                    txtCK.EditValue = xlDT.Rows[0]["CK"].ToFloat();
                    txtThanhtoan.EditValue = xlDT.Rows[0]["Thanhtoan"].ToFloat();
                    txtTongtienSauCK.EditValue = xlDT.Rows[0]["Tongtien"].ToFloat();
                    gcSanpham.DataSource = xgPhieuxuat2ChitietBLL.Phieuxuat2Chitiet_LaytheoId_Phieuxuat2(xgId_Phieuxuat2);
                }
            }
        }

        private void Load_Khachang2()
        {
            Class.gkhtdl63.BLL.Khachhang2BLL xlKhachhang2BLL = new BanHangCshap.Class.gkhtdl63.BLL.Khachhang2BLL();
            DataTable xlDT = xlKhachhang2BLL.Khachhang2_Laydanhsach_View(2);
            if (xlDT != null)
            {
                DataRow dr = xlDT.NewRow();
                dr[0] = DBNull.Value;
                dr[1] = DBNull.Value;
                dr[2] = DBNull.Value;
                dr[3] = DBNull.Value;
                xlDT.Rows.InsertAt(dr, 0);
                gleKhachhang2.Properties.DataSource = xlDT;
                gleKhachhang2Tim.Properties.DataSource = xlDT;
            }
            else
                DevExpress.XtraEditors.XtraMessageBox.Show("Không kết nối được với CSDL", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void LoadNhanvien()
        {
            Class.gkhtdl63.BLL.AllInOneBLL xlAllInOneBLL = new BanHangCshap.Class.gkhtdl63.BLL.AllInOneBLL();
            DataTable xlDT = xlAllInOneBLL.Nhanvien_LayDanhsach();
            if (xlDT != null)
            {
                DataRow xlNewDR = xlDT.NewRow();
                xlNewDR[0] = "";
                xlNewDR[1] = "";
                xlDT.Rows.InsertAt(xlNewDR, 0);

                gleMa_NhanvienLap.Properties.DataSource = xlDT;
                gleMa_NhanvienGiao.Properties.DataSource = xlDT;
            }
            else
                DevExpress.XtraEditors.XtraMessageBox.Show("Không kết nối được với Server\nVui lòng kiểm tra lại kết nối", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void LoadPhieuxuat2()
        {
            object xlId_Khachhang2;
            if (gleKhachhang2Tim.Text.Trim().Equals(""))
            {
                xlId_Khachhang2 = System.Data.SqlTypes.SqlInt64.Null;
            }
            else xlId_Khachhang2 = gleKhachhang2Tim.EditValue;
            gcPhieuxuat2.DataSource = xgPhieuxuat2BLL.Phieuxuat2_Laycodieukien(4,dtpTungay.EditValue, dtpDenngay.EditValue, xlId_Khachhang2);
        }

        private void LoadPhieuxuat2ChitietNULL()
        {
            gcSanpham.DataSource = xgPhieuxuat2ChitietBLL.Phieuxuat2Chitiet_LaytheoId_Phieuxuat2(-1);
        }

        private void LoadQCBoSanphamHiendung()
        {
            Class.gkhtdl63.BLL.QCBoSanphamBLL xlQCBoSanphamBLL = new BanHangCshap.Class.gkhtdl63.BLL.QCBoSanphamBLL();
            xgDTQCBoSanpham = /*xlQCBoSanphamBLL.QCBoSanpham_Laydanhsachhiendung();*/ xlQCBoSanphamBLL.QCBoSanpham_LayDSSanpham();
            if (xgDTQCBoSanpham != null)
            {
                gleMa_QCBoSanpham.DataSource = xgDTQCBoSanpham;
                gleMa_QCBoSanpham.DisplayMember = "Sanpham";
                gleMa_QCBoSanpham.ValueMember = "Ma";
            }
            else
                DevExpress.XtraEditors.XtraMessageBox.Show("Không kết nối được với Server\nVui lòng kiểm tra lại kết nối", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void Taomoi()
        {
            txtTongtien.EditValue = 0;
            txtCK.EditValue = 0;
            txtTongtienSauCK.EditValue = 0;
            txtThanhtoan.EditValue = 0;
            txtConno.EditValue = 0;
            dtpNgay.EditValue = DateTime.Now;
            gleKhachhang2.EditValue = null;
            txtNocu.EditValue = 0;
            gleMa_NhanvienLap.EditValue = UserStatic.tendn;
            gleMa_NhanvienGiao.EditValue = null;
            txtDiachi.EditValue = null;
            txtSoDienthoai.EditValue = null;
            txtGhichu.EditValue = null;
            LoadPhieuxuat2ChitietNULL();
            gleKhachhang2.Focus();
        }

        private void frmBanhang_Load(object sender, EventArgs e)
        {
            dtpTungay.EditValue = DateTime.Now.Date;
            dtpDenngay.EditValue = DateTime.Now.Date;
            Load_Khachang2();
            LoadNhanvien();
            LoadPhieuxuat2();
            LoadQCBoSanphamHiendung();
            Taomoi();
        }

        private void gleKhachhang2_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                Load_Khachang2();
            }
        }

        bool xgGoi = true;

        private void dtpNgay_EditValueChanged(object sender, EventArgs e)
        {
            //if (xgIsAOrE)
            //{
            //    txtMa.EditValue = xgPhieuxuat2BLL.Phieuxuat2_TaomaNgay(dtpNgay.EditValue.ToDateTime(),"TN");
            //}
            if (xgIsAOrE)
            {
                txtMa.EditValue = xgPhieuxuat2BLL.Phieuxuat2_TaomaNgay(dtpNgay.EditValue.ToDateTime(), "TN");

                if (xgGoi)
                {
                    DateTime xx = DateTime.Now;
                    xx = new DateTime(xx.Year, xx.Month, xx.Day, xx.Hour, xx.Minute, 0);
                    int x = DateTime.Compare(dtpNgay.EditValue.ToDateTime(), xx);
                    if (x == -1)
                    {
                        if (gleKhachhang2.EditValue != null)
                        {
                            DanhMuc.gkhtdl63.frmDSPhieuKhachhang2 frm = new frmDSPhieuKhachhang2(dtpNgay.EditValue.ToDateTime(), (long)gleKhachhang2.EditValue.ToFloat());
                            frm.Layngay += new MyEvent_LoadData(frm_Layngay);
                            frm.ShowDialog();
                        }
                    }
                }
            }
        }

        void frm_Layngay(object sender, ELoadData e)
        {
            xgGoi = false;
            dtpNgay.EditValue = e.Data;
            xgGoi = true;

            if (gleKhachhang2.EditValue != null)
            {
                DataTable xlDT = xgPhieuxuat2BLL.Phieuxuat2_TinhNocuKhachhangN(dtpNgay.EditValue.ToDateTime(), (long)gleKhachhang2.EditValue.ToFloat());
                if (xlDT != null)
                {
                    if (xlDT.Rows.Count > 0)
                    {
                        txtDiachi.Text = xlDT.Rows[0]["Diachi"].ToString();
                        txtSoDienthoai.Text = xlDT.Rows[0]["SoDienthoai"].ToString();
                        txtNocu.EditValue = xlDT.Rows[0]["Nocu"].ToString();
                    }
                }
            }
        }

        private bool Kiemtra()
        {
            if (txtMa.Text.Trim().Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Không thể tạo mã phiếu\nVui lòng kiểm tra lại kết nối cơ sở dử liệu", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (gleKhachhang2.Text.Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa chọn khách hàng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                gleKhachhang2.Focus();
                return false;
            }
            if (gleMa_NhanvienGiao.Text.Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa chọn nhân viên trả hàng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                gleMa_NhanvienGiao.Focus();
                return false;
            }
            return true;
        }

        private string xgMa_QCBoSanpham { get; set; }
        //,<Soluong, float,>
        private float xgSoluong { get; set; }
        //,<Ghichu, nvarchar(max),>
        private string xgGhichu { get; set; }
        private float xgCK { get; set; }
        private float xgGia { get; set; }
        private float xgThanhtien { get; set; }
        private string xgDVT { get; set; }

        private void gvSanpham_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            try
            {
                DataRow[] xlDRSanpham = xgDTQCBoSanpham.Select(String.Format("Ma ='{0}'", xgMa_QCBoSanpham));
                if (xlDRSanpham.Length > 0)
                {
                    gvSanpham.SetRowCellValue(e.RowHandle, colMa_QCBoSanpham, xgMa_QCBoSanpham);
                }
                gvSanpham.SetRowCellValue(e.RowHandle, colSoluong, xgSoluong);
                gvSanpham.SetRowCellValue(e.RowHandle, colGhichu, xgGhichu);
                gvSanpham.SetRowCellValue(e.RowHandle, colGia, xgGia);
                gvSanpham.SetRowCellValue(e.RowHandle, colCK, xgCK);
                gvSanpham.SetRowCellValue(e.RowHandle, colThanhtien, xgThanhtien);
                gvSanpham.SetRowCellValue(e.RowHandle, colDVT, xgDVT);
                gvSanpham.UpdateCurrentRow();
            }
            catch (Exception)
            {
            }
        }

        private void gleMa_QCBoSanpham_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            xgMa_QCBoSanpham = "";
            xgSoluong = 0;
            xgGhichu = "";
            xgCK = 0;

            try
            {
                DataRow[] xlDT = xgDTQCBoSanpham.Select(String.Format("Ma ='{0}'", e.NewValue.ToString()));
                if (xlDT.Length > 0)
                {
                    if (!e.NewValue.ToString().Equals(""))
                    {
                        xgMa_QCBoSanpham = e.NewValue.ToString();
                        xgSoluong = 1;
                        xgThanhtien = xgGia = xlDT[0]["Giagoc"].ToFloat();
                        xgDVT = xlDT[0]["DVT"].ToString();
                        xgGhichu = "";
                        xgCK = 0;
                    }
                    try
                    {
                        DataRow xlDR = gvSanpham.GetFocusedDataRow();
                        xgSoluong = xlDR["Soluong"].ToFloat();
                        xgGhichu = xlDR["Ghichu"].ToString();
                        xgCK = xlDR["CK"].ToFloat();
                        xgThanhtien = xgGia * xgSoluong;
                        xgThanhtien = xgThanhtien - xgThanhtien * xgCK / 100;
                    }
                    catch (Exception)
                    {
                    }
                    gvSanpham.SetFocusedRowCellValue(colMa_QCBoSanpham, xgMa_QCBoSanpham);
                    gvSanpham.SetFocusedRowCellValue(colDVT, xgDVT);
                    gvSanpham.SetFocusedRowCellValue(colGia, xgGia);
                    gvSanpham.SetFocusedRowCellValue(colSoluong, xgSoluong);
                    gvSanpham.SetFocusedRowCellValue(colThanhtien, xgThanhtien);
                    gvSanpham.SetFocusedRowCellValue(colCK, xgCK);

                    gvSanpham.UpdateCurrentRow();
                }
            }
            catch (Exception)
            {
            }
        }

        private void gvSanpham_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                if (e.Column.Name.Equals("colSoluong"))
                {
                    xgSoluong = e.Value.ToFloat();

                    xgThanhtien = gvSanpham.GetFocusedRowCellValue("Dongia").ToFloat() * xgSoluong;
                    xgThanhtien = xgThanhtien - xgThanhtien * gvSanpham.GetFocusedRowCellValue("CK").ToFloat() / 100;

                    gvSanpham.SetFocusedRowCellValue(colSoluong, xgSoluong);
                    gvSanpham.SetFocusedRowCellValue(colThanhtien, xgThanhtien);
                }

                if (e.Column.Name.Equals("colGia"))
                {
                    xgGia = e.Value.ToFloat();

                    xgThanhtien = gvSanpham.GetFocusedRowCellValue("Soluong").ToFloat() * xgGia;
                    xgThanhtien = xgThanhtien - xgThanhtien * gvSanpham.GetFocusedRowCellValue("CK").ToFloat() / 100;

                    gvSanpham.SetFocusedRowCellValue(colGia, xgGia);
                    gvSanpham.SetFocusedRowCellValue(colThanhtien, xgThanhtien);
                }

                if (e.Column.Name.Equals("colCK"))
                {
                    xgCK = e.Value.ToFloat();

                    xgThanhtien = gvSanpham.GetFocusedRowCellValue("Soluong").ToFloat() * gvSanpham.GetFocusedRowCellValue("Dongia").ToFloat();
                    xgThanhtien = xgThanhtien - xgThanhtien * xgCK / 100;

                    gvSanpham.SetFocusedRowCellValue(colCK, xgCK);
                    gvSanpham.SetFocusedRowCellValue(colThanhtien, xgThanhtien);
                }
                gvSanpham.UpdateTotalSummary();
            }
            catch (Exception)
            {
            }
        }

        private void gvSanpham_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            try
            {
                gvSanpham.UpdateCurrentRow();
                txtTongtien.EditValue = colThanhtien.SummaryItem.SummaryValue;
                float x = txtTongtien.EditValue.ToFloat();
                txtTongtienSauCK.EditValue = x - x * txtCK.EditValue.ToFloat() / 100;
                txtConno.EditValue = txtNocu.EditValue.ToFloat() - txtTongtienSauCK.EditValue.ToFloat() + txtThanhtoan.EditValue.ToFloat();
            }
            catch (Exception)
            {
            }
        }

        private void gvSanpham_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                gvSanpham.UpdateCurrentRow();
                txtTongtien.EditValue = colThanhtien.SummaryItem.SummaryValue;
                float x = txtTongtien.EditValue.ToFloat();
                txtTongtienSauCK.EditValue = x - x * txtCK.EditValue.ToFloat() / 100;
                txtConno.EditValue = txtNocu.EditValue.ToFloat() - txtTongtienSauCK.EditValue.ToFloat() + txtThanhtoan.EditValue.ToFloat();
            }
            catch (Exception)
            {
            }
        }

        private void gvSanpham_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            gvSanpham.UpdateCurrentRow();
            gvSanpham.UpdateTotalSummary();
        }

        private void btnTaomoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xgIsAOrE = true;
            xgDoubleClick = false;
            Taomoi();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (!Kiemtra()) return;

            Class.gkhtdl63.DAL.Entities.Phieuxuat2 xlPX2 = new BanHangCshap.Class.gkhtdl63.DAL.Entities.Phieuxuat2();
            xlPX2.Ma = txtMa.EditValue.ToString();
            xlPX2.Ngay = dtpNgay.EditValue.ToDateTime();
            xlPX2.Ma_NhanvienGiao = gleMa_NhanvienGiao.EditValue.ToString();
            xlPX2.Id_Khachhang2 = (long)gleKhachhang2.EditValue.ToFloat();
            xlPX2.Tongtien = txtTongtienSauCK.EditValue.ToFloat();
            xlPX2.CK = txtCK.EditValue.ToFloat();
            xlPX2.Thanhtoan = txtThanhtoan.EditValue.ToFloat();
            xlPX2.Ghichu = txtGhichu.Text.ToUpperFirst();
            xlPX2.Loai = 4;

            //Bắt đầu trans
            DatabaseStatic.sqltrans = DatabaseStatic.conn.BeginTransaction();
            bool xlOki = true;

            //Nếu là thêm
            if (xgIsAOrE)
            {
                xlPX2.Ma_NhanvienLap = gleMa_NhanvienLap.EditValue.ToString();
                DataTable KQ = xgPhieuxuat2BLL.Phieuxuat2_Them(xlPX2);
                Class.gkhtdl63.DAL.Entities.Phieuxuat2Chitiet xlPX2CT = new BanHangCshap.Class.gkhtdl63.DAL.Entities.Phieuxuat2Chitiet();
                if (KQ != null)
                {
                    xgId_Phieuxuat2 = (long)KQ.Rows[0][0].ToFloat();
                    int xlCount = gvSanpham.RowCount - 1;
                    for (int i = 0; i < xlCount; i++)
                    {
                        DataRow xlR = gvSanpham.GetDataRow(i);
                        xlPX2CT.Id_Phieuxuat2 = xgId_Phieuxuat2;
                        xlPX2CT.Ma_QCBoSanpham = xlR["Ma_QCBoSanpham"].ToString();
                        xlPX2CT.DVT = xlR["DVT"].ToString();
                        xlPX2CT.Soluong = xlR["Soluong"].ToFloat();
                        xlPX2CT.Dongia = xlR["Dongia"].ToFloat();
                        xlPX2CT.CK = xlR["CK"].ToFloat();
                        xlPX2CT.Ghichu = xlR["Ghichu"].ToString();
                        xlPX2CT.Thanhtien = xlR["Thanhtien"].ToFloat();
                        if (!xgPhieuxuat2ChitietBLL.Phieuxuat2Chitiet_Them(xlPX2CT))
                        {
                            xlOki = false;
                            break;
                        }
                    }
                }
                else
                {
                    xlOki = false;
                }
            }
            else
            {//sửa thì dớt em này
                xlPX2.Ma_NhanvienLap = xgMa_NhanvienLap;
                xlPX2.Ngay = xgNgay;
                xlPX2.Id = xgId_Phieuxuat2;
                if (xgPhieuxuat2ChitietBLL.Phieuxuat2Chitiet_Xoa(xgId_Phieuxuat2))
                {
                    if (xgPhieuxuat2BLL.Phieuxuat2_Capnhat(xlPX2))
                    {
                        Class.gkhtdl63.DAL.Entities.Phieuxuat2Chitiet xlPX2CT = new BanHangCshap.Class.gkhtdl63.DAL.Entities.Phieuxuat2Chitiet();
                        int xlCount = gvSanpham.RowCount - 1;
                        for (int i = 0; i < xlCount; i++)
                        {
                            DataRow xlR = gvSanpham.GetDataRow(i);
                            xlPX2CT.Id_Phieuxuat2 = xgId_Phieuxuat2;
                            xlPX2CT.Ma_QCBoSanpham = xlR["Ma_QCBoSanpham"].ToString();
                            xlPX2CT.DVT = xlR["DVT"].ToString();
                            xlPX2CT.Soluong = xlR["Soluong"].ToFloat();
                            xlPX2CT.Dongia = xlR["Dongia"].ToFloat();
                            xlPX2CT.CK = xlR["CK"].ToFloat();
                            xlPX2CT.Ghichu = xlR["Ghichu"].ToString();
                            xlPX2CT.Thanhtien = xlR["Thanhtien"].ToFloat();
                            if (!xgPhieuxuat2ChitietBLL.Phieuxuat2Chitiet_Them(xlPX2CT))
                            {
                                xlOki = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        xlOki = false;
                    }
                }
                else xlOki = false;
            }

            if (xlOki)
            {
                DatabaseStatic.sqltrans.Commit();
                UltitityStatic.thongBaoThanhCong("Đã lưu thành công.");
                xgIsAOrE = false;
                LoadPhieuxuat2();
                LoadQCBoSanphamHiendung();
            }
            else
            {
                DatabaseStatic.sqltrans.Rollback();
                UltitityStatic.thongBaoLoi("Không thể lưu. Xin kiểm tra lại thông tin.");
            }
        }

        private bool xgDoubleClick = false;
        private void gvPhieuxuat2_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                xgId_Phieuxuat2 = (long)gvPhieuxuat2.GetFocusedRowCellValue("Id").ToFloat();
                xgIsAOrE = false;
                xgDoubleClick = true;
                Hienthi();
            }
            catch (Exception)
            {
            }

        }

        private void gleKhachhang2_EditValueChanged(object sender, EventArgs e)
        {//Xem chú thích ở bán hàng
            if (gleKhachhang2.EditValue != null)
            {
                if (xgIsAOrE)
                {
                    DateTime xx = DateTime.Now;
                    xx = new DateTime(xx.Year, xx.Month, xx.Day, xx.Hour, xx.Minute, 0);
                    int x = DateTime.Compare(dtpNgay.EditValue.ToDateTime(), xx);
                    if (x == -1)
                    {
                        if (xgIsAOrE)
                        {
                            DanhMuc.gkhtdl63.frmDSPhieuKhachhang2 frm = new frmDSPhieuKhachhang2(dtpNgay.EditValue.ToDateTime(), (long)gleKhachhang2.EditValue.ToFloat());
                            frm.Layngay += new MyEvent_LoadData(frm_Layngay);
                            frm.ShowDialog();
                        }
                    }
                    else
                    {
                        DataTable xlDT = xgPhieuxuat2BLL.Phieuxuat2_TinhNocuKhachhangN(dtpNgay.EditValue.ToDateTime(), (long)gleKhachhang2.EditValue.ToFloat());
                        if (xlDT != null)
                        {
                            if (xlDT.Rows.Count > 0)
                            {
                                txtDiachi.Text = xlDT.Rows[0]["Diachi"].ToString();
                                txtSoDienthoai.Text = xlDT.Rows[0]["SoDienthoai"].ToString();
                                txtNocu.EditValue = xlDT.Rows[0]["Nocu"].ToString();
                            }
                        }
                    }
                }
                else
                {
                    DataTable xlDT = xgPhieuxuat2BLL.Phieuxuat2_TinhNocuKhachhangN(dtpNgay.EditValue.ToDateTime(), (long)gleKhachhang2.EditValue.ToFloat());
                    if (xlDT != null)
                    {
                        if (xlDT.Rows.Count > 0)
                        {
                            txtDiachi.Text = xlDT.Rows[0]["Diachi"].ToString();
                            txtSoDienthoai.Text = xlDT.Rows[0]["SoDienthoai"].ToString();
                            txtNocu.EditValue = xlDT.Rows[0]["Nocu"].ToString();
                        }
                    }
                }
            }
        }

        private void gcSanpham_DataSourceChanged(object sender, EventArgs e)
        {
            try
            {
                gvSanpham.UpdateCurrentRow();
                txtTongtien.EditValue = colThanhtien.SummaryItem.SummaryValue.ToFloat();
                txtConno.EditValue = txtNocu.EditValue.ToFloat() - txtTongtienSauCK.EditValue.ToFloat() + txtThanhtoan.EditValue.ToFloat();
            }
            catch (Exception)
            {
            }
        }

        private void txtNocu_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                txtConno.EditValue = txtNocu.EditValue.ToFloat() - txtTongtienSauCK.EditValue.ToFloat() + txtThanhtoan.EditValue.ToFloat();
            }
            catch (Exception)
            {
            }
        }

        private void txtCK_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            try
            {
                float x = txtTongtien.EditValue.ToFloat();
                txtTongtienSauCK.EditValue = x - x * e.NewValue.ToFloat() / 100;
                txtConno.EditValue =txtNocu.EditValue.ToFloat()- txtTongtienSauCK.EditValue.ToFloat() + txtThanhtoan.EditValue.ToFloat();
            }
            catch (Exception)
            {
            }
        }

        private void txtThanhtoan_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            try
            {
                txtConno.EditValue = txtNocu.EditValue.ToFloat() - txtTongtienSauCK.EditValue.ToFloat() + e.NewValue.ToFloat();
            }
            catch (Exception)
            {

            }
        }

        private void gleKhachhang2Tim_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                LoadPhieuxuat2();
            }
        }

        private void btnThemKhachhang_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DanhMuc.gkhtdl63.Khachhang2.frmNCC2Chitiet frm = new BanHangCshap.DanhMuc.gkhtdl63.Khachhang2.frmNCC2Chitiet(true, null);
            frm.Tailai += new MyEvent_LoadData(frm_Tailai);
            frm.ShowDialog();
        }

        void frm_Tailai(object sender, ELoadData e)
        {
            Load_Khachang2();
            gleKhachhang2.EditValue = e.Data;
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            WaitDialogForm dg = new WaitDialogForm("Đang load dữ liệu...");
            Report.gkhtdl63.frmMainRPTS frm = new BanHangCshap.Report.gkhtdl63.frmMainRPTS();
            Report.gkhtdl63.MyRPT.Xuat.rptPhieuxuatA5 rpt = new BanHangCshap.Report.gkhtdl63.MyRPT.Xuat.rptPhieuxuatA5("PHIẾU TRẢ HÀNG",txtNocu.Text, txtConno.Text, txtCK.EditValue.ToDecimal());
            rpt.DataSource = xgPhieuxuat2BLL.Phieuxuat2_In(xgId_Phieuxuat2);
            frm.SetReport(rpt);
            dg.Close();
            frm.ShowDialog();
        }

        private void btnXoaphieu_Click(object sender, EventArgs e)
        {
            try
            {
                string Ma = "";
                if (DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có chắc muốn xóa phiếu này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Ma = gvPhieuxuat2.GetFocusedRowCellValue("Ma").ToString();
                    if (!xgPhieuxuat2BLL.Phieuxuat2_Xoa((long)gvPhieuxuat2.GetFocusedRowCellValue("Id").ToFloat()))
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("Không thể xóa, Vui lòng kiểm tra lại kết nối CSDL", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        LoadPhieuxuat2();
                        LoadQCBoSanphamHiendung();
                        if (Ma.Equals(txtMa.Text.Trim()))
                        {
                            Taomoi();
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DataRow row = gvSanpham.GetFocusedDataRow();
                row.Delete();
                gvSanpham.UpdateCurrentRow();
            }
            catch (Exception)
            {
            }
        }
    }
}
